#include"../include/head.h"
int logrecord(char *username,char *cmd,int x){

    //记录传入的用户和操作
    record record_log;

    //设置检查命令
    char check[20]={"checklog"};

    strcpy(record_log.username,username);
    
    //登陆标记为1，加入日志表
    if(x==1){
        strcpy(record_log.cmd,"clinet join");
        insert_log(record_log);
        return 0;
    }

    //退出标记为2，加入日志表
    if(x==2){
        strcpy(record_log.cmd,"clinet exit");
        insert_log(record_log);
        return 0;
    }
    strcpy(record_log.cmd,cmd);
    
    /* //插入日志 */
    insert_log(record_log);

    //判断是否为查表命令
    if(strcmp(record_log.cmd,check)==0){
        check_log();
    }
    
    return 0;
}
int insert_log(record re_log)
{
	MYSQL *conn;
	char *server = "localhost";
	char *user = "chen";
	char *password = "123";
	char *database = "37chen";//要访问的数据库名称
    char query[200]={0};
	
    sprintf(query,"insert into log ( username, cmd ) values ('%s','%s')",re_log.username,re_log.cmd);
    
    int queryResult;

	conn = mysql_init(NULL);

	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
	}
    else
    {
		printf("Connected...\n");
	}

	queryResult = mysql_query(conn, query);
	if(queryResult)
	{
		printf("Error making query:%s\n", mysql_error(conn));
	}
    else
    {
		printf("insert success\n");
	}
	mysql_close(conn);

	return 0;
}
int check_log()
{
	MYSQL *conn;
	MYSQL_RES *res;
	MYSQL_ROW row;
	char *server = "localhost";
	char *user = "root";
	char *password = "";
	char *database = "ftp";//要访问的数据库名称
	char query[300] = "select id,username,cmd,time from log";
	unsigned int queryRet;
	
    //在输出前先打印查询语句
	puts(query);

    //初始化
	conn = mysql_init(NULL);
    if(!conn)
    {
        printf("MySQL init failed\n");
        return -1;
    }

    //连接数据库，看连接是否成功，只有成功才能进行后面的操作
	if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
	{
		printf("Error connecting to database: %s\n", mysql_error(conn));
		return -1;
	}
    else
    {
		printf("MySQL Connected...\n");
	}

    //把SQL语句传递给MySQL
	queryRet = mysql_query(conn, query);
	if(queryRet)
    {
        printf("Error making query: %s\n", mysql_error(conn));
    }
    else
    {

        //用mysql_num_rows可以得到查询的结果集有几行
        //要配合mysql_store_result使用
        //第一种判断方式
        res = mysql_store_result(conn);
        printf("mysql_num_rows = %lu\n", (unsigned long)mysql_num_rows(res));

        //第二种判断方式,两种方式不能一起使用
        /* res = mysql_use_result(conn); */

        row = mysql_fetch_row(res);
        if(NULL == row)
        {
            printf("Don't find any data\n");
        }
        else
        {
            do
            {	
                /* printf("num=%d\n",mysql_num_fields(res));//列数 */

                //每次for循环打印一整行的内容
                for(queryRet = 0; queryRet < mysql_num_fields(res); ++queryRet)
                {
                    printf("%8s ", row[queryRet]);
                }

                printf("\n");

            }while(NULL != (row = mysql_fetch_row(res)));
        }

        mysql_free_result(res);
    }

    mysql_close(conn);

    return 0;
}